home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / dkbuts.zip / GLUETGA.C < prev    next >
C/C++ Source or Header  |  1991-05-16  |  6KB  |  208 lines

  1. /*****************************************************************
  2.  
  3. GLUETGA.C  -  Glues together several partial .TGA files
  4.               (using the header Y offset for line #) and
  5.           concatenates them into one full screen .TGA
  6.           file of whatever resolution used.  Nearly any
  7.           resolution should work, but all partial files
  8.           MUST be of the same overall frame X-Y size.
  9.  
  10.           By Aaron A. Collins, written on 6/2/90
  11.  
  12.           This file is released to the Public Domain.
  13.  
  14.           PICLAB is a trademark of The Stone Soup Group.
  15.           TARGA is a trademark of AT&T/Truevision.
  16.  
  17. Revised 05/09/91 V1.1 AAC - reordered args so output file is last. 
  18. Revised 05/15/91 V1.2 AAC - removed IBM-ness of filenames/exts.
  19.  
  20. *****************************************************************/
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25.  
  26. #define HDRLEN 18L
  27.  
  28. char outname[80];
  29. char inname[20][80];
  30.  
  31. void main(argc,argv)
  32. int argc;
  33. char *argv[];
  34. {
  35.     int xres, yres, xhi, yhi, yorigin, yorghi, outxres, outyres, numfiles;
  36.     int x, y, index;
  37.     unsigned char *buffer;
  38.     long posn;
  39.     FILE *in, *out;
  40.  
  41.     printf("\n\nTARGA-24 Image File Concatenation Utility\n");
  42.     printf("Version 1.2 By Aaron A. Collins.  Written 3/30/90 Revised 05/15/91\n\n");
  43.  
  44.     if (argc < 3)
  45.     {
  46.         printf("Usage: %s InputFile1 [InputFile2]... OutputFile\n\n",argv[0]);
  47.         exit(1);
  48.     }
  49.  
  50.     numfiles = argc - 2;          /* number of input files to do */
  51.  
  52.     for (x = 0; x < numfiles; x++)
  53.         strcpy(&inname[x][0], argv[1 + x]); /* get in filename(s) */
  54.  
  55.     strcpy(outname, argv[numfiles + 1]);  /* get final output filename */
  56.  
  57.     if ((in = fopen(&inname[0][0], "rb")) == NULL) /* try 1st file ext */
  58.     {
  59.         printf("ERROR - Couldn't open file %s\n", argv[1]);
  60.         exit(1);
  61.     }
  62.  
  63.     if ((out = fopen(outname, "wb")) == NULL)
  64.     {
  65.         printf("ERROR - Couldn't create file %s\n", outname);
  66.         fclose(in);
  67.         exit(1);
  68.     }
  69.  
  70.     printf("Creating Output file = %s\n\n", outname); /* show stats */
  71.  
  72.     /** Copy 1st part of standard Targa header **/
  73.  
  74.     for (x = 0; x < 10; x++)    /* 00, 00, 02, then 7 00's... */
  75.         fputc(fgetc(in), out);
  76.  
  77.     /** discard y orgin, and rewrite 0 as new in rest of Targa hdr **/
  78.  
  79.     fgetc(in);
  80.     fgetc(in);
  81.     fputc(0, out);
  82.     fputc(0, out);
  83.     
  84.     /** load x and y resolution, and rewrite as rest of Targa hdr **/
  85.  
  86.     outxres = fgetc(in);            /* lo order */
  87.     xhi = fgetc(in);            /* hi order */
  88.     outxres += ((unsigned int) xhi) << 8;
  89.  
  90.     fputc(outxres & 0x00ff, out);        /* write lo order */
  91.     fputc((outxres & 0xff00) >> 8, out);    /* write hi order */
  92.  
  93.     outyres = fgetc(in);            /* now do yres the same... */
  94.     yhi = fgetc(in);
  95.     outyres += ((unsigned int) yhi) << 8;
  96.  
  97.     fputc(outyres & 0x00ff, out);
  98.     fputc((outyres & 0xff00) >> 8, out);
  99.  
  100.     fputc(fgetc(in), out);    /* 24 bits/pixel (16 million colors!) (24) */
  101.     fputc(fgetc(in), out);    /* Set bit to indicate top-down display (32)*/
  102.     
  103.     fclose(in);        /* Close the input file, read body later. */
  104.     
  105.     printf("Image  X  resolution = %d\n", outxres);     /* show more stats */
  106.     printf("Image  Y  resolution = %d\n\n", outyres);
  107.  
  108.     if ((buffer = malloc(outxres * 3)) == NULL)
  109.     {
  110.         printf("ERROR - couldn't allocate memory for buffer!\n");
  111.         fclose (out);
  112.         unlink (outname);
  113.     }
  114.  
  115.     /* write out zeroes to the entire output file's body */
  116.  
  117.     for (x = 0; x < outxres; x++)        /* clear line buffer */
  118.     {
  119.         index = x * 3;
  120.         buffer[index++] = '\0';        /* write null BGR triples */
  121.         buffer[index++] = '\0';
  122.         buffer[index] = '\0';
  123.     }
  124.     
  125.     for (y = 0; y < outyres; y++)         /* for the number of lines */
  126.         if (fwrite(buffer, 3, outxres, out) != (unsigned int)outxres) /*wrt a line*/
  127.         {
  128.             printf("ERROR - Couldn't create file %s - out of disk space?\n", outname);
  129.             fclose(in);
  130.             fclose(out);
  131.             unlink(outname);
  132.             exit(1);
  133.         }
  134.  
  135.     for (index = 0; index < numfiles; index++) /* for # of input files */
  136.     {
  137.         if ((in = fopen(&inname[index][0], "rb")) == NULL)
  138.         {
  139.             printf("ERROR - Couldn't open file %s\n", &inname[index][0]);
  140.             fclose(out);
  141.             unlink(outname);
  142.             exit(1);
  143.         }
  144.         printf("Glueing File:  %s\t", &inname[index][0]);
  145.  
  146.         /** Read and discard 1st part of standard Targa header **/
  147.  
  148.         for (x = 0; x < 10; x++)   /* 00, 00, 02, then 7 00's... */
  149.             fgetc(in);
  150.  
  151.         /** load y origin for this segment... **/
  152.  
  153.         yorigin = fgetc(in);
  154.         yorghi = fgetc(in);
  155.         yorigin += ((unsigned int) yorghi) << 8;
  156.         
  157.         /** load x and y resolution, check if okay... **/
  158.  
  159.         xres = fgetc(in);            /* lo order */
  160.         xhi = fgetc(in);            /* hi order */
  161.         xres += ((unsigned int) xhi) << 8;
  162.         if (xres > outxres)            /* too big? */
  163.         {
  164.             printf("ERROR - X res. of %s (%d) exceeds maximum (%d)!\n", &inname[index][0], xres, outxres);
  165.             fclose(in);            /* close files */
  166.             fclose(out);
  167.             unlink(outname);        /* delete bad file */
  168.             exit(1);
  169.         }
  170.  
  171.         yres = fgetc(in);        /* now do yres the same... */
  172.         yhi = fgetc(in);
  173.         yres += ((unsigned int) yhi) << 8;
  174.         if (yres > outyres)            /* too big? */
  175.         {
  176.             printf("ERROR - Y res. of %s (%d) exceeds maximum (%d)!\n", &inname[index][0], yres, outyres);
  177.             fclose(in);            /* close files */
  178.             fclose(out);
  179.             unlink(outname);        /* delete bad file */
  180.             exit(1);
  181.         }
  182.  
  183.         fgetc(in); /* absorb 24 bits/pixel (16 million colors!) (24)*/
  184.         fgetc(in); /* also bit to indicate top-down display (32)*/
  185.  
  186.         printf("to line:   %d", yorigin);
  187.  
  188.         posn = (long) yorigin * ((long) outxres * 3) + HDRLEN;
  189.         fseek(out, posn, 0);    /* position output file for copy */
  190.  
  191.         for (y = yorigin; y < yres; y++) /* for every line in file */
  192.         {
  193.             printf("\b\b\b%3d", y);       /* disp. current line # */
  194.  
  195.             /* read in a whole line */
  196.             if (fread(buffer, 3, xres, in) != (unsigned int)xres)
  197.                 break;        /* stop if less bytes read */
  198.  
  199.             fwrite(buffer, 3, xres, out); /* copy BGR triplets */
  200.         }
  201.         printf("\n");
  202.         fclose(in);        /* close this input file */
  203.     }
  204.     fflush(out);            /* close output file when done */
  205.     fclose(out);
  206.     exit(0);
  207. }
  208.